﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class SearchFilteredSalesOrders
    {
        partial void SearchFilteredSalesOrders_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.Application.User.HasPermission(Permissions.CanEditSalesOrder))//admin
            {

            }
            else if (this.Application.User.HasPermission(Permissions.CanSearchShop) && this.Application.User.HasPermission(Permissions.CanManageMySalesOrder))
            {
                string userName = this.Application.User.FullName;
                //sales person
                foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.SelectedSalesPerson = item;
                        //this.FindControl("SelectedSalesPersion").IsReadOnly = true;
                        break;
                    }
                }

                //customer
                foreach (Customer item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.SelectedCustomer = item;
                        //this.FindControl("SelectedCustomer").IsReadOnly = true;
                        break;
                    }
                }
            }
            else
            {
                this.FindControl("Search").IsReadOnly = true;
            }
        }

        partial void Search_Execute()
        {
            FilteredSalesOrders.Load();
        }

        partial void ClearFilter_Execute()
        {
            if (!this.FindControl("SelectedSalesPerson").IsReadOnly)
            {
                this.SelectedSalesPerson = null;
            }

            if (!this.FindControl("SelectedCustomer").IsReadOnly)
            {
                this.SelectedCustomer = null;
            }

            if (!this.FindControl("SelectedShop").IsReadOnly)
            {
                this.SelectedShop = null;
            }

            this.SelectedId = null;
            this.SelectedDateFrom = null;
            this.SelectedDateTo = null;
            this.SelectedInvoiceNumber = null;

        }

        partial void SearchFilteredSalesOrders_Activated()
        {
            try
            {
                this.FindControl("SelectedSalesPersion").IsEnabled = (this.SelectedSalesPerson == null);
                this.FindControl("SelectedCustomer").IsEnabled = (this.SelectedCustomer == null);
            }
            catch (Exception)
            {
            }
        }
    }
}
